/*
Copyright (c) 2007-2009, Yusuke Yamamoto
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
 * Neither the name of the Yusuke Yamamoto nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.ch_linghu.fanfoudroid.fanfou;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.util.Log;

import com.ch_linghu.fanfoudroid.http.HttpException;
import com.ch_linghu.fanfoudroid.http.Response;

/**
 * A data class representing one single status of a user.
 * 
 * @author Yusuke Yamamoto - yusuke at mac.com
 */
public class Status extends WeiboResponse implements java.io.Serializable {
	private static final long serialVersionUID = 1608000492860584608L;

 
	private User user = null;
	
	private String id;
	private String weiboId;
	private String uid;
	private String content;
	private Date ctime;
	private int from;
	private int comment;
	private int transpondId;
	private int transpond;
	private int type;
	
//	private String thumbUrl;
//	private String thumbMiddleUrl;
    private String fromData;
    private int isDel;
    private int city;
    private int bid;
    private String bname;
    private boolean favorited;
    private String uname;
    private String face;
    private String transpondData;
    private Date timeStamp;
    
	private boolean isFavorited;
	private String thumbnail_pic;
	private String bmiddle_pic;
	private String original_pic;
    
	/* package */Status(Response res, Weibo weibo) throws HttpException {
		super(res);
		Element elem = res.asDocument().getDocumentElement();
		init(res, elem, weibo);
	}

	/* package */Status(Response res, Element elem, Weibo weibo)
			throws HttpException {
		super(res);
		init(res, elem, weibo);
	}

	Status(Response res) throws HttpException {
		super(res);
		JSONObject json = res.asJSONObject();
		try {
			 
			weiboId = json.getString("weibo_id");
			id = json.getString("weibo_id");
			uid = json.getString("uid");
			content = json.getString("content");
			ctime =  parseDate(json.getString("ctime"),"yyyy-MM-dd HH:mm");
			from = json.getInt("from");
			comment = json.getInt("comment");
			transpondId = json.getInt("transpond_id");
			transpond = json.getInt("transpond");
			type = json.getInt("type");
			city = json.getInt("city");
			bid = json.getInt("bid");
			bname =json.getString("bname");
			
			
			if (!json.isNull("user")){ 
				user = new User(json.getJSONObject("user"));
			}
			if(type==1){
				if(!json.isNull("type_data")){
					JSONObject data=	json.getJSONObject("type_data");
					thumbnail_pic = data.getString("thumburl");
					bmiddle_pic = data.getString("thumbmiddleurl");
					original_pic = data.getString("picurl");
					
				}
			}
			
 
		} catch (JSONException je) {
			throw new HttpException(je.getMessage() + ":" + json.toString(), je);
		}

	}

	/* modify by sycheng add some field */
	public Status(JSONObject json) throws HttpException, JSONException {
		
		
		weiboId = json.getString("weibo_id");
		uid = json.getString("uid");
		content = json.getString("content");
		ctime =  parseDate(json.getString("ctime"),"yyyy-MM-dd HH:mm");
		from = json.getInt("from");
		comment = json.getInt("comment");
		transpondId = json.getInt("transpond_id");
		transpond = json.getInt("transpond");
		type = json.getInt("type");
		
		city = json.getInt("city");
		bid = json.getInt("bid");
		bname =json.getString("bname");
		
		user = new User(json.getJSONObject("user"));
		if(type==1){
			if(!json.isNull("type_data")){
				JSONObject data=	json.getJSONObject("type_data");
				thumbnail_pic = data.getString("thumburl");
				bmiddle_pic = data.getString("thumbmiddleurl");
				original_pic = data.getString("picurl");
				
			}
		}
 
	}

	public Status(String str) throws HttpException, JSONException {
		// StatusStream uses this constructor
		super();
		JSONObject json = new JSONObject(str);
		
		weiboId = json.getString("weibo_id");
		uid = json.getString("uid");
		content = json.getString("content");
		ctime =  parseDate(json.getString("ctime"),"yyyy-MM-dd HH:mm");
		from = json.getInt("from");
		comment = json.getInt("comment");
		transpondId = json.getInt("transpond_id");
		transpond = json.getInt("transpond");
		type = json.getInt("type");
		
		city = json.getInt("city");
		bid = json.getInt("bid");
		bname =json.getString("bname");
		
		user = new User(json.getJSONObject("user"));
		
		if(type==1){
			if(!json.isNull("type_data")){
				JSONObject data=	json.getJSONObject("type_data");
				thumbnail_pic = data.getString("thumburl");
				bmiddle_pic = data.getString("thumbmiddleurl");
				original_pic = data.getString("picurl");
				
			}
			
		}

		
 
	}

	private void init(Response res, Element elem, Weibo weibo)
			throws HttpException {
		//ensureRootNodeNameIs("status", elem);
		user = new User(res, (Element) elem.getElementsByTagName("user")
				.item(0), weibo);
		weiboId = getChildText("weibo_id", elem);
		uid = getChildText("uid", elem);
		content = getChildText("content", elem);
		ctime =  parseDate(getChildText("ctime", elem),"yyyy-MM-dd HH:mm");
		from =getChildInt("from", elem);
		comment = getChildInt("comment", elem);
		transpondId = getChildInt("transpond_id", elem);
		transpond = getChildInt("transpond", elem);
		type = getChildInt("type", elem);
		
		city = getChildInt("city", elem);
		bid = getChildInt("bid", elem);
		bname =getChildText("bname", elem);
		
		if(type==1){
			NodeList datas = elem.getElementsByTagName("type_data");
			if (datas.getLength() != 0) {
				Element data = (Element) datas.item(0);
				thumbnail_pic = getChildString("thumburl", data);
				bmiddle_pic = getChildString("thumbmiddleurl", data);
				original_pic = getChildString("picurl", data);
			}
		}
		
 
	
 
	}
	
	public String getWeiboId() {
		return weiboId;
	}

	public String getUid() {
		return uid;
	}

	public String getContent() {
		return content;
	}

	public Date getCtime() {
		return ctime;
	}

	public int getFrom() {
		return from;
	}

	public int getComment() {
		return comment;
	}

	public int getTranspondId() {
		return transpondId;
	}

	public int getTranspond() {
		return transpond;
	}

 

	public String getFromData() {
		return fromData;
	}

	public int getCity() {
		return city;
	}

	public int getBid() {
		return bid;
	}
	
	public String getBname(){
		return bname;
	}

	public boolean getFavorited() {
		return favorited;
	}

	public String getUname() {
		return uname;
	}

	public String getFace() {
		return face;
	}

	public String getTranspondData() {
		return transpondData;
	}

	public Date getTimeStamp() {
		return timeStamp;
	}

	 

	/**
	 * Returns the id of the status
	 * 
	 * @return the id
	 */
	public String getId() {
		return this.weiboId;
	}

	 
	/**
	 * Test if the status is favorited
	 * 
	 * @return true if favorited
	 * @since Weibo4J 1.0.4
	 */
	public boolean isFavorited() {
		return isFavorited;
	}

	public String getThumbnail_pic() {
		return thumbnail_pic;
	}

	public String getBmiddle_pic() {
		return bmiddle_pic;
	}

	public String getOriginal_pic() {
		return original_pic;
	}

	/**
	 * Return the user
	 * 
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	// TODO: 等合并Tweet, Status
	public int getType() {
		return -1111111;
	}

 
 

	/* package */
	static List<Status> constructStatuses(Response res, Weibo weibo)
			throws HttpException {

		Document doc = res.asDocument();
		if (isRootNodeNilClasses(doc)) {
			return new ArrayList<Status>(0);
		} else {
			try {
				ensureRootNodeNameIs("statuses", doc);
				NodeList list = doc.getDocumentElement().getElementsByTagName(
						"status");
				int size = list.getLength();
				List<Status> statuses = new ArrayList<Status>(size);
				for (int i = 0; i < size; i++) {
					Element status = (Element) list.item(i);
					statuses.add(new Status(res, status, weibo));
				}
				return statuses;
			} catch (HttpException te) {
				ensureRootNodeNameIs("nil-classes", doc);
				return new ArrayList<Status>(0);
			}
		}

	}

	/* modify by sycheng add json call method */
	/* package */
	static List<Status> constructStatuses(Response res) throws HttpException {
		
		
		try {
			 
			JSONArray list = res.asJSONArray();
			Log.d("dada-json",list.toString());
			int size = list.length();
			List<Status> statuses = new ArrayList<Status>(size);
			for (int i = 0; i < size; i++) {
				statuses.add(new Status(list.getJSONObject(i)));
			}
			return statuses;
		} catch (JSONException jsone) {
			throw new HttpException(jsone);
		} catch (HttpException te) {
			throw te;
		}
	}

	@Override
	public int hashCode() {
		return id.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (null == obj) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		// return obj instanceof Status && ((Status) obj).id == this.id;
		return obj instanceof Status && this.id.equals(((Status) obj).id);
	}

	@Override
	public String toString() {
//		return "Status{" + "createdAt=" + createdAt + ", id=" + id + ", text='"
//				+ text + '\'' + ", source='" + source + '\'' + ", isTruncated="
//				+ isTruncated + ", inReplyToStatusId=" + inReplyToStatusId
//				+ ", inReplyToUserId=" + inReplyToUserId + ", isFavorited="
//				+ isFavorited + ", thumbnail_pic=" + thumbnail_pic
//				+ ", bmiddle_pic=" + bmiddle_pic + ", original_pic="
//				+ original_pic + ", inReplyToScreenName='"
//				+ inReplyToScreenName + '\'' + ", latitude=" + latitude
//				+ ", longitude=" + longitude + ", retweetDetails="
//				+ retweetDetails + ", user=" + user + '}';
 
		
		return "Status{" + "weiboId=" + weiboId + ", uid=" + uid + ", content='"
		+ content + '\'' + ", ctime='" + ctime + '\'' + ", from="
		+ from + ", comment=" + comment
		+ ", transpondId=" + transpondId + ", transpond="
		+ transpond + ", type=" + type
		+ ", thumbnail_pic=" + thumbnail_pic + ", bmiddle_pic="
		+ bmiddle_pic +", original_pic=" + original_pic + ", fromData='"
		+ fromData + '\'' + ", isDel=" + isDel
		+ ", city=" + city + ", bid="
		+ bid + ", favorited=" + favorited +
		", bname=" + bname +", city=" + city +
		", uname=" + uname + ", face="+
		face + ", transpondData=" + transpondData +
		", timeStamp=" + timeStamp +  
		'}';
	}

	public boolean isEmpty() {
		return (null == id);
	}
}
